package com.aike.service;

import com.aike.dao.GradeMapper;
import com.aike.dao.TaskMapper;
import com.aike.dao.UserMapper;
import com.aike.pojo.Meta;
import com.aike.pojo.User;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService{

    private UserMapper userMapper;

    private TaskMapper taskMapper;

    private GradeMapper gradeMapper;

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public void setTaskMapper(TaskMapper taskMapper) {
        this.taskMapper = taskMapper;
    }

    public void setGradeMapper(GradeMapper gradeMapper) {
        this.gradeMapper = gradeMapper;
    }

    public String register(String username, String password, Integer gender, String classname) {
        JSONObject object = new JSONObject();
        int v = userMapper.register(username, password, gender, classname);
        if(v > 0)
        {
            object.put("meta", new Meta(200, "注册成功"));
            object.put("data", "null");
        }
        else
        {
            object.put("meta", new Meta(400, "注册失败"));
            object.put("data", "null");
        }
        return object.toJSONString();
    }

    public String login(String username, String password) {
        JSONObject object = new JSONObject();
        User user = userMapper.login(username, password);
        if(user != null)
        {
            object.put("meta", new Meta(200, "登录成功"));
            object.put("data", user);
        }
        else
        {
            object.put("meta", new Meta(400, "登录失败"));
            object.put("data", "null");
        }
        return object.toJSONString();
    }

    public String getUserById(Integer userId) {
        JSONObject object = new JSONObject();
        User user = userMapper.getUserById(userId);
        if(user != null)
        {
            object.put("meta", new Meta(200, "获取成功"));
            object.put("data", user);
        }
        else
        {
            object.put("meta", new Meta(400, "获取失败，该用户不存在"));
            object.put("data", "null");
        }
        return object.toJSONString();
    }

    public String getUserByType(Integer type) {
        JSONObject object = new JSONObject();
        Map<String, Object> map = new HashMap<String, Object>();
        List<User> list = userMapper.getUserByType(type);
        map.put("list", list);

        object.put("data", map);
        object.put("meta", new Meta(200, "获取成功"));

        return object.toJSONString();
    }

    public String updateUserById(Integer userId, String username, String password, Integer gender, String classname, Integer type) {
        JSONObject object = new JSONObject();

        int v = userMapper.updateUserById(userId, username, password, gender, classname, type);
        if(v > 0)
        {
            object.put("meta", new Meta(200, "更新成功"));
            object.put("data", "null");
        }
        else
        {
            object.put("meta", new Meta(400, "更新失败"));
            object.put("data", "null");
        }
        return object.toJSONString();
    }

    public String deleteUserById(Integer userId) {
        JSONObject object = new JSONObject();
        User user = userMapper.getUserById(userId);
        int v = 0;
        //如果数据不存在
        if(user == null)
        {

        }
        //如果是学生则删除所有有关成绩
        else if(user.getType() == 1)
        {
            gradeMapper.deleteGradeByStudentId(userId);
            v = userMapper.deleteUserById(userId);
        }
        //如果是老师则删除所有有关成绩和课题
        else if(user.getType() == 2)
        {
            gradeMapper.deleteGradeByTeacherId(userId);
            taskMapper.deleteTaskByTeacherId(userId);
            v = userMapper.deleteUserById(userId);
        }

        if(v > 0)
        {
            object.put("meta", new Meta(200, "删除成功"));
            object.put("data", "null");
        }
        else
        {
            object.put("meta", new Meta(400, "删除失败"));
            object.put("data", "null");
        }
        return object.toJSONString();
    }


}
